Security News
Research
Supply Chain Attack on Rspack npm Packages Injects Cryptojacking Malware
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
@types/json-stable-stringify
Advanced tools
TypeScript definitions for json-stable-stringify
@types/json-stable-stringify is a TypeScript type definition package for the json-stable-stringify library. This library provides a way to stringify JavaScript objects in a consistent and stable order, which is useful for tasks like comparing JSON objects or generating consistent hash values.
Stable Stringification
This feature allows you to stringify a JavaScript object in a stable order. The keys in the resulting JSON string are sorted, ensuring consistent output for the same input object.
{"typescript":"import stringify from 'json-stable-stringify';\n\nconst obj = { b: 1, a: 2 };\nconst stableString = stringify(obj);\nconsole.log(stableString); // Output: '{\"a\":2,\"b\":1}'"}
Custom Comparator
This feature allows you to provide a custom comparator function to control the order of keys in the resulting JSON string. In this example, the keys are sorted in reverse order.
{"typescript":"import stringify from 'json-stable-stringify';\n\nconst obj = { b: 1, a: 2 };\nconst stableString = stringify(obj, { cmp: (a, b) => a.key < b.key ? 1 : -1 });\nconsole.log(stableString); // Output: '{\"b\":1,\"a\":2}'"}
Custom Replacer
This feature allows you to provide a custom replacer function to filter or modify values during the stringification process. In this example, the key 'b' is excluded from the resulting JSON string.
{"typescript":"import stringify from 'json-stable-stringify';\n\nconst obj = { b: 1, a: 2, c: 3 };\nconst stableString = stringify(obj, { replacer: (key, value) => key === 'b' ? undefined : value });\nconsole.log(stableString); // Output: '{\"a\":2,\"c\":3}'"}
fast-json-stable-stringify is another library that provides stable JSON stringification. It is known for its performance and simplicity. Compared to json-stable-stringify, it is often faster but offers fewer customization options.
json-stringify-deterministic is a library that ensures deterministic JSON stringification. It is similar to json-stable-stringify but focuses on providing a deterministic output without additional features like custom comparators or replacers.
canonical-json is a library that provides canonical JSON stringification, ensuring a consistent and stable order of keys. It is similar to json-stable-stringify but follows the canonical JSON specification more strictly.
npm install --save @types/json-stable-stringify
This package contains type definitions for json-stable-stringify (https://github.com/substack/json-stable-stringify).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/json-stable-stringify.
/**
* Deterministic version of JSON.stringify() so you can get a consistent hash from stringified results.
*
* @returns Deterministic json result.
*/
declare function stringify(obj: any, opts?: stringify.Comparator | stringify.Options): string;
declare namespace stringify {
interface Element {
key: string;
value: any;
}
type Comparator = (a: Element, b: Element, opts?: { get(k: string): any }) => number;
type Replacer = (key: string, value: any) => any;
interface Options {
/**
* Custom comparator for key
*/
cmp?: Comparator;
/**
* Indent the output for pretty-printing.
*
* Supported is either a string or a number of spaces.
*/
space?: string | number;
/**
* Option to replace values to simpler values
*/
replacer?: Replacer;
/**
* true to allow cycles, by marking the entries as __cycle__.
*/
cycles?: boolean;
}
}
export = stringify;
These definitions were written by Matt Frantz.
FAQs
TypeScript definitions for json-stable-stringify
The npm package @types/json-stable-stringify receives a total of 2,021,306 weekly downloads. As such, @types/json-stable-stringify popularity was classified as popular.
We found that @types/json-stable-stringify demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.
Security News
Sonar’s acquisition of Tidelift highlights a growing industry shift toward sustainable open source funding, addressing maintainer burnout and critical software dependencies.